﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace NHibernateToSQLMapping.DAL
{
    public abstract class SQLServerDAL : IMasterDAL
    {
        /// <summary>
        /// 查询数据库名
        /// </summary>
        /// <param name="conStr">连接字符串</param>
        /// <returns></returns>
        public List<string> GetDataBaseName(string conStr)
        {
            List<string> lst = new List<string>();
            string sql = "SELECT Name FROM SysDatabases ORDER BY Name";
            DataTable dt = SqlHelper.ExecuteGetTable(conStr, System.Data.CommandType.Text, sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    lst.Add(dr[0].ToString());
                }
            }
            return lst;
        }

        /// <summary>
        /// 查询所有表名
        /// </summary>
        /// <param name="conStr">连接字符串</param>
        /// <returns></returns>
        public List<string> GetTableName(string conStr)
        {
            string sql = "SELECT Name FROM SysObjects Where XType='U' ORDER BY Name";
            List<string> lst = new List<string>();
            DataTable dt = SqlHelper.ExecuteGetTable(conStr, System.Data.CommandType.Text, sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    lst.Add(dr[0].ToString());
                }
            }
            return lst;
        }

        /// <summary>
        /// 查询所有视图名
        /// </summary>
        /// <param name="conStr">连接字符串</param>
        /// <returns></returns>
        public List<string> GetViewName(string conStr)
        {
            string sql = "SELECT Name FROM SysObjects Where XType='V' ORDER BY Name";
            List<string> lst = new List<string>();
            DataTable dt = SqlHelper.ExecuteGetTable(conStr, System.Data.CommandType.Text, sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    lst.Add(dr[0].ToString());
                }
            }
            return lst;
        }

        /// <summary>
        /// 查询所有字段属性
        /// </summary>
        /// <param name="conStr">连接字符串</param>
        /// <returns></returns>
        public abstract DataSet GetTableInfo(string conStr, string tableName);
    }
}
